package com.yangle.frame.admin.listener;

import com.yangle.frame.admin.entity.FrameCode;
import com.yangle.frame.admin.entity.FrameCodeItem;
import com.yangle.frame.admin.service.FrameCodeItemService;
import com.yangle.frame.admin.service.FrameCodeService;
import com.yangle.frame.common.service.RedisServiceImpl;
import com.yangle.frame.common.utils.EhcacheUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;

import java.util.List;

/**
 * Created by yangle on 2018/6/5.
 */
public class TaskRunner implements ApplicationRunner, Ordered {
    private static final Logger logger = LoggerFactory.getLogger(TaskRunner.class);
    @Autowired
    private FrameCodeService frameCodeService;
    @Autowired
    private FrameCodeItemService frameCodeItemService;
    @Autowired
    private RedisServiceImpl redisService;
    @Autowired
    private EhcacheUtils ehcacheUtils;
    @Override
    public int getOrder() {
        return 2;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {

        logger.info("=====================系统启动=====================");
        logger.info("=====================清除缓存记录=====================");
        ehcacheUtils.clear();
        List<FrameCode> frameCodes = frameCodeService.getAll(new FrameCode());
        logger.info("=================开始初始化代码项====================");
        for (FrameCode frameCode:frameCodes){
            String code = frameCode.getCode();
            FrameCodeItem frameCodeItem=new FrameCodeItem();
            frameCodeItem.setCodeId(frameCode.getId());
            List<FrameCodeItem> frameCodeItems = frameCodeItemService.getAll(frameCodeItem);
            ehcacheUtils.update(code,frameCodeItems);
        }
        logger.info("=================结束代码项初始化====================");
    }
}